package mya.mes.mdc.logic.dispatch.executor;

import mya.mes.mdc.model.dispatch.MdcDispatchContent;
import mya.mes.mdc.weixin.WxUploadManager;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class DispatchExecutor {

    private final DispatchCurrent dispatchCurrent = new DispatchCurrent();
    private static DispatchExecutor _instance = new DispatchExecutor();
    private ScheduledExecutorService executorService = null;

    private DispatchExecutor() {

    }


    public static DispatchExecutor getInstance() {
        return _instance;
    }


    public synchronized void start() throws Exception {
        if (executorService == null) {
            WxUploadManager.getInstance().start();
            executorService = Executors.newScheduledThreadPool(1);
            dispatchCurrent.updateCache();
            executorService
                    .scheduleWithFixedDelay(new DispatchTimerTask(),
                            1L, 2000L, TimeUnit.MILLISECONDS);
        }
    }


    public void queueDispatch(MdcDispatchContent dispatchContent) {
        if (dispatchContent == null) {
            return;
        }
        dispatchCurrent.queueDispatch(dispatchContent);
    }

    DispatchCurrent getDispatchCurrent() {
        return dispatchCurrent;
    }
}
